$(function(){
	   
})


/* 组件示例-选择机构 */
var deptRows = []
function selectDept() {
	var url =  ctx + 'tool/example/selectDeptExa';
    var options = {
        title: '选择部门',
        width: "480",
        url: url,
        callBack: doSubmit
    };
    var deptIds = []
    deptRows.forEach(function (dept) {
        deptIds.push(dept.deptId)
    })

    $.modal.openOptionsInitData(options,deptIds);
}

function doSubmit(index, layero) {
	var rows = layero.find('iframe')[0].contentWindow.$._tree.getCheckedNodes()
    deptRows = []
	var deptText = ''
    rows.forEach(function(row) {
        deptRows.push({
            deptName: row.name,
            deptId: row.id
        })
        deptText += row.name +'，'
    })
    console.log('rows',deptRows)
    $('#treeName').val(deptText.slice(0,deptText.length-1))
    layer.close(index);
	
	/* 获取客户列表 */
	selectUser() 
	$('#autoSelect').attr("disabled",!Boolean(deptRows.length)) //选了机构之后才能选择用户
	
	selectUserMu()
	$('#autoSelectMu').attr("disabled",!Boolean(deptRows.length)) //选了机构之后才能选择用户
}

/* 组件示例-选择用户单选 */
function selectUser(){
	var list = []
    /* 获取所有用户用于搜索 */
	$.ajax({
        url:  ctx + 'admin/user/page',
//        data: {"param": request.term},
        data: {size: 10, current: 1},
        type: 'GET',
        success: function (data) {
        	list = data.data.records.map(function (item) {
                return {
                	label: item.loginname + '-' + item.username, 
                	value: item.loginname + '-' + item.username, 
                	userId: item.userId
                	}
            });
        	
        	
        	/* 初始化搜索input框  */
        	$("#autoSelect").autocomplete({
        		//minLength: 2,
                	  autoFocus: true,
            	select: function (event, ui) {
                    console.log('选中的选项',ui);
                    console.log($(event.target).val()); //获取输入框对象
                },
//                source: list,
                source: function(request, response){
                	var term = request.term;
                	var result = $.ui.autocomplete.filter( list, term )
                    if (result.length > 0) {
                        response(result);
                    }else{
                    	response(['null']);
                        $(".ui-autocomplete").html('<div style="margin:10px auto;text-align: center;color: #999;"><i class="fa fa-exclamation"></i> 无匹配结果</div>');
                    }
                },
                focus: function() {
                    // 防止在获得焦点时插入值
                    return false;
                  },
            }).click(function () {
                $(this).autocomplete("search");
            });
        	
        	
        },
       
    });
		
	
	
}

/* 组件示例-选择用户多选 */
function selectUserMu(){
	var list = []
    /* 获取所有用户用于搜索 */
	$.ajax({
        url:  ctx + 'admin/user/page',
//        data: {"param": request.term},
        data: {size: 10, current: 1},
        type: 'GET',
        success: function (data) {
        	list = data.data.records.map(function (item) {
                return {
                	label: item.loginname + '-' + item.username, 
                	value: item.loginname + '-' + item.username, 
                	userId: item.userId
                	}
            });
        	
        	
        	/* 初始化搜索input框  */
        	$("#autoSelectMu").on( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                        $( this ).data( "ui-autocomplete" ).menu.active ) {
                      event.preventDefault();
                    }
                  }).autocomplete({
        		//minLength: 2,
                	  autoFocus: true,
            	select: function (event, ui) {
//                    console.log('选中的选项',ui);
//                    console.log($(event.target).val()); //获取输入框对象
                    
                    var terms = split( this.value );
                    // 移除当前所有的内容
                    terms.pop();
                    if(terms.indexOf(ui.item.value) == -1){ //判断是否已选中过当前点击的选项
                    	// 将选中的添加到input中
                        terms.push( ui.item.value );
                        // 在最后添加分隔符
                        terms.push( "" );
                        this.value = terms.join( ", " );
                    }
                   
                    return false;
                
                },
//                source: list,
                source: function(request, response){
                	var term = request.term;
                	var result = $.ui.autocomplete.filter( list, extractLast( term ) )
                    if (result.length > 0) {
                        response(result);
                    }else{
                    	response(['null']);
                        $(".ui-autocomplete").html('<div style="margin:10px auto;text-align: center;color: #999;"><i class="fa fa-exclamation"></i> 无匹配结果</div>');
                    }
                	
                	
                },
                focus: function() {
                    // 防止在获得焦点时插入值
                    return false;
                  },
            }).click(function () {
                $(this).autocomplete("search");
            });
        	
        },
       
    });
		
	

}

function split( val ) {
    return val.split( /,\s*/ );
 }
function extractLast( term ) {
    return split( term ).pop();
  }